package cn.biplam.back.operator.ddl.sql2md;

import java.util.ArrayList;

import cn.biplam.back.sql.dml.Delete;
import cn.biplam.back.sql.dml.Insert;
import cn.biplam.back.sql.dml.Update;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.interfaces.DataOperator;
import cn.biplam.common.opcode.sqlengine.DDLParserCode.TabBufCode;
import cn.biplam.common.struc.MdStruc;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;

/**
 * 源数据表缓冲区数据操作类 
 * <li>根据表ID查询表缓存信息
 * <li>获取当前批次ID下表缓存信息
 * <li>插入一个表到源数据表缓存区
 * <li> 删除缓存区表信息
 * <li> 根据表ID更新源数据表缓存信息
 * @author XiaoRuxing 2009-6-26下午08:49:26
 * @version 1.0
 */
public class TableBuffer extends DataOperator{
	
	public TableBuffer() throws ErrorMessage{
		super("table_buffer");
	}
	
	@Override
	protected OperatorResult _exchange() throws ErrorMessage {
		ArrayList<String> sql = new ArrayList<String>();
		if(opId.equals(TabBufCode.Insert)){
			this.insert(sql, filter);
			return this.doing(sql, Operator_Type.DML);
		}else if(this.opId.equals(TabBufCode.Update)){
			this.update(sql, filter);
			return this.doing(sql, Operator_Type.DML);
		}else if(this.opId.equals(TabBufCode.Delete)){
			this.deleteByID(sql, filter);
			return this.doing(sql, Operator_Type.DML);
		}else if(this.opId.equals(TabBufCode.Get_By_Table_ID)){
			this.selectById(sql, filter);
			return this.doing(sql, Operator_Type.Select);
		}else if(this.opId.equals(TabBufCode.Get_By_Batch)){
			this.selectByBatch(sql, filter);
			return this.doing(sql, Operator_Type.Select);
		}else if(this.opId.equals(TabBufCode.Get_By_Name)){
			this.selectByName(sql, filter);
			return this.doing(sql, Operator_Type.Select);
		}else if(this.opId.equals(TabBufCode.Delete_By_TaskId)){
			this.deleteByTaskID(sql, filter);
			return this.doing(sql, Operator_Type.DML);
		}else{
			throw new ErrorMessage("Data Operator id not found:"+ opId+" !");
		}
	}
	
	/** 根据表ID查询表缓存信息 
	 * @throws ErrorMessage */
	public void selectById(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		_init(filter);
		StringBuilder sp=new StringBuilder();
		sp.append(" select *	                ");
		sp.append("   from "+this.tableName+" t ");
		sp.append("  where "+MdStruc.SrcTabBufferStruc.TABLE_ID+"='");
		sp.append(this.filter.getValue(MdStruc.SrcTabBufferStruc.TABLE_ID));
		sp.append("'");
		sql.add(sp.toString());
	}

	/** 根据批次ID查询表缓存信息 
	 * @throws ErrorMessage */
	public void selectByBatch(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		_init(filter);
		StringBuilder sp=new StringBuilder();
		sp.append(" select * 		 ");
		sp.append("   from "+this.tableName+" t ");
		sp.append("  where "+MdStruc.SrcTabBufferStruc.BATCH+"='");
		sp.append(this.filter.getValue(MdStruc.SrcTabBufferStruc.BATCH));
		sp.append("'");
		sql.add(sp.toString());
	}
	
	/** 根据表名称和批次ID查询表缓存信息 
	 * @throws ErrorMessage */
	public void selectByName(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		_init(filter);
		StringBuilder sp=new StringBuilder();
		sp.append(" select   * 		 ");
		sp.append("   from "+this.tableName+" t     ");
		sp.append("  where "+MdStruc.SrcTabBufferStruc.TABLE_NAME+"='");
		sp.append(this.filter.getValue(MdStruc.SrcTabBufferStruc.TABLE_NAME));
		sp.append("' and ");
		sp.append(MdStruc.SrcTabBufferStruc.BATCH+"='");
		sp.append(this.filter.getValue(MdStruc.SrcTabBufferStruc.BATCH));
		sp.append("'");
		sql.add(sp.toString());
	}
	
	/**
	 * 根据表ID更新源数据表缓存信息 
	 * @param sql    
	 * @param filter
	 * @throws ErrorMessage
	 */
	public void update(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		_init(filter);
		Update update=new Update(this.tableName);
		update.putStringFieldIgnoreNull(MdStruc.SrcTabBufferStruc.TABLE_NAME,filter.getValue(MdStruc.SrcTabBufferStruc.TABLE_NAME));
		update.putStringFieldIgnoreNull(MdStruc.SrcTabBufferStruc.TABLE_CN_NAME, filter.getValue(MdStruc.SrcTabBufferStruc.TABLE_CN_NAME));
		update.putStringFieldIgnoreNull(MdStruc.SrcTabBufferStruc.TABLE_COMMENTS, filter.getValue(MdStruc.SrcTabBufferStruc.TABLE_COMMENTS));
		update.putStringFieldIgnoreNull(MdStruc.SrcTabBufferStruc.THEME_ID, filter.getValue(MdStruc.SrcTabBufferStruc.THEME_ID));
		update.setWhere(MdStruc.SrcTabBufferStruc.TABLE_ID+"='"+filter.getValue(MdStruc.SrcTabBufferStruc.TABLE_ID)+"'");
		sql.add(update.getSimpleSql());
	}
	
	/**
	 * 插入一个表到源数据表缓存区域
	 * @param sql    sql容器
	 * @param filter 需要插入的表信息
	 * @throws ErrorMessage
	 */
	public void insert(ArrayList<String> sql,DataFilter filter) throws ErrorMessage {
		_init(filter);
		String woid=generatorRowID();
		Insert insert=new Insert(this.tableName);
		insert.putStringFieldDenyNull(MdStruc.SrcTabBufferStruc.TABLE_ID, woid);
		insert.putStringFieldIgnoreNull(MdStruc.SrcTabBufferStruc.THEME_ID, filter.getValue(MdStruc.SrcTabBufferStruc.THEME_ID));
		insert.putStringFieldDenyNull(MdStruc.SrcTabBufferStruc.TABLE_NAME, filter.getValue(MdStruc.SrcTabBufferStruc.TABLE_NAME));
		insert.putStringFieldIgnoreNull(MdStruc.SrcTabBufferStruc.TABLE_CN_NAME, filter.getValue(MdStruc.SrcTabBufferStruc.TABLE_CN_NAME));
		insert.putStringFieldIgnoreNull(MdStruc.SrcTabBufferStruc.TABLE_COMMENTS, filter.getValue(MdStruc.SrcTabBufferStruc.TABLE_COMMENTS));
		insert.putStringFieldDenyNull(MdStruc.SrcTabBufferStruc.BATCH, filter.getValue(MdStruc.SrcTabBufferStruc.BATCH));
		sql.add(insert.getSimpleSql());
	}
	
	/** 
	 * 删除缓存区表信息<br>
	 */
	public void deleteByID(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		_init(filter);
		String tableId=null;
		tableId=this.filter.getValue(MdStruc.SrcTabBufferStruc.TABLE_ID);
		Delete delete=new Delete(this.tableName);
		delete.setWhere(MdStruc.SrcTabBufferStruc.TABLE_ID+"='"+tableId+"'");
		sql.add(delete.getSimpleSql());
	}
	
	/** 
	 * 删除缓存区表信息<br>
	 */
	public void deleteByTaskID(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		_init(filter);
		String taskId=null;
		taskId=filter.getValue(MdStruc.SrcTabBufferStruc.BATCH);
		/**删除列sql*/
		new TableColBuffer().deleteByTaskID(sql, filter);
		/**删除表sql*/
		Delete delete=new Delete(this.tableName);
		delete.setWhere(MdStruc.SrcTabBufferStruc.BATCH+"='"+taskId+"'");
		sql.add(delete.getSimpleSql());
	}
	
	/**
	 * 根据模型类型初始化表名  
	 */
	private void _init(DataFilter filter) throws ErrorMessage{
		String modelType = filter.getValue(MdStruc.SrcTabBufferStruc.MODEL_TYPE);
		if(TOOLS.StringIsEmpty(modelType)){
			throw new ErrorMessage("Model type is null");
		}
		this.tableName = "src_table_buffer";
	}
}
